<?php
/**
 * 项目：红河移动电路资源管理信息系统
 *
 * 公司：红河移动分公司
 *
 * @author 李朝应
 *
 * E-mail:lichaoying.hh@163.com
 *
 */
class Org2_model extends CI_Model {
   function __construct()
    {
        parent::__construct();
    }


   function setOrg2($org1_id,$name,$fzr,$email,$tel){
    $data = array(
               'org2_name' => $name,
               'org2_fzr' => $fzr,
               'org2_email' => $email,
               'org2_tel' => $tel,
               'org1_id' => $org1_id
            );

    $this->db->insert('org2', $data);
    return $this->db->affected_rows() ;

   }
 function updateOrg2($id,$org1_id,$name,$fzr,$email,$tel){
    $data = array(
               'org2_name' => $name,
               'org2_fzr' => $fzr,
               'org2_email' => $email,
               'org2_tel' => $tel,
               'org1_id' => $org1_id
            );
      $this->db->where('org2_id',$id);
      $this->db->update('org2', $data);
    return $this->db->affected_rows() ;
 }
function delOrg2($id){
    $this->db->delete('org2', array('org2_id' => $id));
	return true;
 }

function getCount(){
      return  $this->db->count_all("org2");
   }

 function getOrg2All(){
       $sql="SELECT * FROM crms_org2, crms_org1 WHERE crms_org2.org1_id = crms_org1.org1_id";
       $query = $this->db->query($sql);
		if ($query->num_rows() > 0)
		{
			  return $query; //返回查询结果
        }
      }
    function getOrg2($id){
     $this->db->where("org2_id", $id);
     $query = $this->db->get('org2');
		if ($query->num_rows() > 0)
		{
			 return $query->row_array(); //返回查询结果
        }
   }
   function hasOrg2($org1_id,$name){
        $this->db->where("org1_id", $org1_id);
        $this->db->where("org2_name", $name);
		$query = $this->db->get('org2');
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
   }
   function hasSameRec($id,$org1_id,$name){
        $this->db->where('org2_id !=', $id);
        $this->db->where("org1_id", $org1_id);
        $this->db->where("org2_name", $name);
		$query = $this->db->get('org2');
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}else{
           	return FALSE;
        }
   }
   function getOrg2InJson($org1_id){
        $json_string = "{msgs:[";
        $this->db->select("org2_id as id ,org2_name as name");
        $this->db->where("org1_id", $org1_id);
		$query = $this->db->get('org2');
        foreach ($query->result_array() as $row) {
          $json_string.=json_encode($row).','  ;
          }
      //  $json_string=substr($json_string,0,-1);
        $json_string .= "]}";
        return $json_string;
        //{msgs:[{"id":"7","name":"\u7f51\u7edc\u90e8"},]} 返回的JSON格式
        //
        //Jquery读写时在IE中出错,FF正常
        /*$("#org1_name").change(function(){
          $.getJSON('http://localhost/crms/index.php/admin/findOrg2/5',function(json){
             var msgs=json.msgs;
             var oPt="";
            for( var i=0;i<msgs.length;i++){
               oPt +='<option vaule='+msgs[i].id+'>'+msgs[i].name+'</option>';
            }
           $('#org2_name').html(oPt);
             });
          */
        
   }
   function getOrg2In($org1_id){
        $this->db->select("org2_id as id ,org2_name as name");
        $this->db->where("org1_id", $org1_id);
		$query = $this->db->get('org2');
         $oPt=" <option value='NULL' selected='selected'>[-请选择任一项-]</option>";
        foreach ($query->result_array() as $row) {
         $oPt .= "<option value=" .$row['id'].">".$row['name']."</option>";
         }
        return  $oPt;
   }
     function getOrg2In2($org1_id){
        $this->db->select("org2_id as id ,org2_name as name");
        $this->db->where("org1_id", $org1_id);
		$query = $this->db->get('org2');
         $oPt=" <option value='NULL' selected='selected'>[-请选择任一项-]</option>";
        foreach ($query->result_array() as $row) {
         $oPt .= "<option value=" .$row['id'].">[ + ] ".$row['name']."</option>";
         }
        return  $oPt;
   }
}//类定义结束
?>
